package com.rubiksillusions;

public class PatternCreator {
	public static void main(String[] args) {
		String finalStrings[] = {
			".....b.............bb.bb..b.gg.gg.gg...oooooo..rrrr.rr",
			".r.................bbbbb..b.gg.gg.gg...oooooo..r.rr.rr"
		};
		
		String initStrings[] = {
//			".....r..........................................b.....",
//			
//			".b...................r................................",
//			
//			".......b......................r.......................",
//			".......r......................b.......................",
//			
//			"...b.................................r................",
//			"...r.................................b................",
			
			".........................r....................b......."
		};
		
		String[] masks = {
//			"ww.w.wwww.........g..g.....b..b..b..ooo.........r..r..", // Cubelet 6, 0, 2 (1, 3, 5, 7)
//			"ww.w.www..........g..g........b..b..ooo.........r.....", // Cubelet 6, 0 (1, 3, 5, 7)
//			".w.w.wwww............g.....b..b..b...oo.........r..r..", // Cubelet 2, 0 (1, 3, 5, 7)
//			"ww.w.w.ww.........g..g.....b..b.....bo..........r..r..", // Cubelet 6, 2 (1, 3, 5, 7)
//			"ww.w.w.w..........g..g........b.....oo..........r.....", // Cubelet 6 (1, 3, 5, 7)
//			".w.w.www.............g........b..b...oo.........r.....", // Cubelet 0 (1, 3, 5, 7)
//			".w.w.w.ww............g.....b..b......o..........r..r..", // Cubelet 2 (1, 3, 5, 7)
			".........wwwwwwwww.bb.bb..b.gg.gg.gg...oooooo..r.rr.rr" // Nessun Cubelet (1, 3, 5, 7)
		};
		
		for (int k = 0; k < masks.length; k++) {
			for (int j = 0; j < initStrings.length; j++) {
				String output = getJavaForPattern(
					"",
					initStrings[j],
					finalStrings,
					masks[k]
				);
				if (output != null && !output.equals("")) System.out.println(output);
			}
			System.out.println("\n\n");
		}
	}
	
	private static String getJavaForPattern(String name, String initString, String[] finalStrings, String mask) {
		String javaString = "";
		String moves = getMovesForPattern(initString, finalStrings, mask);
		if (moves == null) return "";
		if (moves.equals("")) return "";
		javaString += "if (!executed) executed = rubiksCube.executeStatusMovesList(" +
			"\"" + initString + "\", " + "\"" +
			moves + "\", " + "true);";
		return javaString;
	}
	
	private static String getMovesForPattern(String initString, String[] finalStrings, String mask) {
		int MAX_MOVES = 7;
		String tempInitString = "";
		String[] tempFinalStrings = new String[finalStrings.length];
		for (int i = 0; i < tempFinalStrings.length; i++) {
			tempFinalStrings[i] = "";
		}
		
		
		for (int i = 0; i < mask.length(); i++) {
			if (mask.charAt(i) != '.') {
				if (initString.charAt(i) != '.') return null;
				tempInitString += mask.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += mask.charAt(i);
				}
				
			} else {
				tempInitString += initString.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += finalStrings[j].charAt(i);
				}
			}
		}
		int i = 0, movesCount = 0;
		while (movesCount <= MAX_MOVES) {
			int temp = i;
			String out = "";
			out += getMoveByNumber(temp % 18) + " ";
			while (temp / 18 != 0) {
				temp = temp / 18;
				out += getMoveByNumber(temp % 18) + " ";
			}
			
			String[] splitted = out.split(" ");
			movesCount = splitted.length;
			if (movesCount > MAX_MOVES) break;
			
			RubiksCube initCube = new RubiksCube(tempInitString);
			initCube.executeMovesList(out, true);
			
			String exCube = initCube.getFaceletsString();
			if (i % 10000 == 0) System.out.println(i + ": " + out);
			//System.out.println(exCube + " " + out);
			i++;
			
			for (int j = 0; j < tempFinalStrings.length; j++) {
				//System.out.println("    " + tempFinalStrings[j]);
				if (exCube.equals(tempFinalStrings[j])) {
					return out;
				}
			}
		}
		return "";
	}
	
	private static String getMoveByNumber(int number) {
		switch (number) {
			case 0: return "R";
			case 1: return "L";
			case 2: return "U";
			case 3: return "D";
			case 4: return "F";
			case 5: return "B";
			case 6: return "R'";
			case 7: return "L'";
			case 8: return "U'";
			case 9: return "D'";
			case 10: return "F'";
			case 11: return "B'";
			case 12: return "R2";
			case 13: return "L2";
			case 14: return "U2";
			case 15: return "D2";
			case 16: return "F2";
			case 17: return "B2";
		}
		return "";
	}
}
